

/**
 * 
 * @author Simon Auner 2011-11-28
 * @param {Object} cfg The config object
 * @param {DOMElement} cfg.parentEl The parent element, containing the countdown
 * @param {Function} cfg.callback Callback to execute when the countdown reaches 0
 */
function CountdownTimer(cfg) {
    
    var me = this;
    me.parentEl = cfg.parentEl;
    
    me.el = me.parentEl.appendChild(document.createElement("div"));
    
    me.el.className = "countdownTimer";
    
    
    me.callback = cfg.callback;
    
    
}

CountdownTimer.prototype = {

/**
 * Sets new countdown value
 * @author Simon Auner 2011-11-28
 */
    getNewTime : function() {
        this.timeLeft = this.endTime.getTime() - new Date();
    },

/**
 * The new timer, set to run on an interval
 * @author Simon Auner 2011-11-28
 * 
 */
    timer: function () {
        
        var me = this;
        
        // if we should keep counting, get a new time
        if(me.keepCounting) {
            me.getNewTime();
        }
        
        // if we've reached 0, stop counting and execute callback
        if(me.timeLeft <= 0) {
            
            // stop the interval
            me.keepCounting = false;
            
            // truncate time left to 0
            me.timeLeft = 0;
            
            // clear interval
            clearInterval(me.timerInterval);
            
            me.el.style[$.bt] = "translate3d(0, 50px, 0)";
            me.el.style.opacity = "0";
            
            
            // execute callback
            if(typeof me.callback !== "undefined") {
                me.callback();
            }
        }
        
        // always print output
        me.setOutput();
        
        
        
    },
/**
 * Formats the output in seconds
 * @author Simon Auner 2011-11-28
 * 
 * @param {Number} time Time in milliseconds
 * @returns {Number} Time in seconds (integer)
 */
    formatOutput : function(time) {
        
        // get seconds from milliseconds
        return parseInt(time / 1000, 10); 
        
    },
    
    setText : function(txt) {
        this.el.innerHTML = txt;
    },
    
/**
 * Prints output to the DOM element
 * @author Simon Auner 2011-11-28
 */
    setOutput : function() {
        
        this.el.innerHTML = this.formatOutput(this.timeLeft);
    },
/**
 * Clears the callback of the timer
 * @author Simon Auner 2011-11-28
 */
    clearCallback : function() {
        delete this.callback;
    },

/**
 * Returns the timer start value in seconds
 * @author Simon Auner 2011-11-28
 * @returns {Number} The timer start value
 */
    getTimerStartValue : function() {
        return this.timerStartValue;
    },

/**
 * Start the timer countdown
 * @author Simon Auner 2011-11-28
 * @param {Number} t The number of seconds to countdown from
 */
    start: function (t) {
        var me = this;
        
        me.timerStartValue = t;
        
        me.keepCounting = true;
        
        me.currentTime = new Date();
        me.endTime = new Date();
        me.endTime.setMilliseconds(me.endTime.getMilliseconds() + t + 500);
        
        me.timeLeft = t;
        
        me.setOutput();
        
        me.el.style[$.bt] = "translate3d(0, 0, 0)";
        me.el.style.opacity = "1";
        
        // set up an interval (store it in me to be able to clear later)
        setTimeout(function() {
            me.timerInterval = setInterval(function() {
                me.timer();
            }, 250);
        }, 500);
    },
    
    /**
     * Stops the time
     * @author Aram Taieb 2011-11-30
     * @modified 
     */
    stop : function() {
        clearInterval(this.timerInterval);
    },
    
    shake : function() {
        var el = this.el,
        style = el.style;
        
        var callback = function() {
            
            el.removeEventListener($.bae, callback, false);
            style[$.ban] = "";
        };
        
        el.addEventListener($.bae, callback, false);
        
        style[$.ban] = "shake";
        style[$.bad] = "500ms";
    }
};